<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\home\controller;

use api\index\controller\CommonController;
use app\admin\model\ChangeCoinModel;
use app\admin\model\FavoriteModel;
use app\admin\model\InfoSetModel;
use app\admin\model\MemberModel;
use app\admin\model\ReadLookModel;
use app\admin\model\VideoModel;
use cmf\controller\RestBaseController;
use think\Db;
//视频
class VideoController extends RestBaseController
{
    //视频分类
    public function videoCategoryList(){
        if($this->request->isGet()){
            $res = CommonController::selectData('video_category','id,video_name','sort desc');
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    //视频轮播列表
    public function videoRotationList(){
        if($this->request->isGet()){
            $video_category_id = $this->request->get('video_category_id');//分类id
            $rule = config('verify.video_rotation_list');
            $this->checkValidate(['video_category_id'=>$video_category_id],$rule);

            $res = CommonController::selectWhereData('video',['video_category_id'=>$video_category_id,'is_rotation'=>VideoModel::ROTATION_YES],'id,title,video_url,thumbnail');
            foreach($res as &$value){
                $value['video_url'] = CommonController::absolutionUrl($value['video_url']);
                $value['thumbnail'] = CommonController::absolutionUrl($value['thumbnail']);
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    //视频列表
    public function videoList(){
        if($this->request->isGet()){
            $video_category_id = $this->request->get('video_category_id');//分类id
            $page = $this->request->get('page');//分页页码
            $rule = config('verify.video_list');
            $this->checkValidate(['video_category_id'=>$video_category_id,'page'=>$page],$rule);

            $res = CommonController::selectWherePageData('video',['video_category_id'=>$video_category_id],$page,'id,title,video_url,is_task,thumbnail,look_num,create_time');
            //查询登录人收藏的记录
            $member_id = $this->getUserId();
            $where = [
                'member_id' => $member_id,
                'type' => FavoriteModel::COLLECTION,
                'relation_type' => FavoriteModel::VIDEO_TYPE
            ];
            $res_collection = CommonController::selectWhereData('favorite',$where,'id,relation_id');
            foreach($res as &$value){
                $value['video_url'] = CommonController::absolutionUrl($value['video_url']);
                $value['thumbnail'] = CommonController::absolutionUrl($value['thumbnail']);
                $value['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
                //是否收藏
                $value['is_collection'] = FavoriteModel::NO;//否
                foreach($res_collection as $c_value){
                    if($value['id'] == $c_value['relation_id']){
                        $value['is_collection'] = FavoriteModel::YES;//是
                        break;
                    }
                }
            }
            $total_page = CommonController::totalWherePage('video',['video_category_id'=>$video_category_id]);
            $this->success('成功',['data'=>$res,'last_page'=>$total_page]);
        }else{
            $this->error('请求方式错误');
        }
    }

    //视频浏览
    public function browse(){
        if($this->request->isPost()){
            $video_id = $this->request->post('video_id');//视频id
            $rule = config('verify.video_collection');
            $this->checkValidate(['video_id'=>$video_id],$rule);

            //检查视频是否有效
            $is_valid = CommonController::findWhereData('video',['id'=>$video_id],'id');
            if(!$is_valid){
                $this->error('该视频不存在');
            }

            $videoModel = new VideoModel();
            $res = $videoModel->where('id',$video_id)->setInc('look_num',1);
            if($res){
                $this->success('浏览成功');
            }else{
                $this->error('浏览失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    //观看视频获取球币
    public function videoIncCoin(){
        if($this->request->isPost()){
            $video_id = $this->request->post('video_id');//视频id
            $rule = config('verify.video_collection');
            $this->checkValidate(['video_id'=>$video_id],$rule);

            //检查视频是否有效
            $is_valid = CommonController::findWhereData('video',['id'=>$video_id],'id');
            if(!$is_valid){
                $this->error('该视频不存在');
            }
            $member = MemberModel::get($this->getUserId());
            if ($member->task(ReadLookModel::TYPE_LOOK)){
                $this->error('今日已完成视频任务');
            }
            Db::startTrans();
            $video_coin = InfoSetModel::value('video_coin');
            $res = $member->setInc('coin',$video_coin);
            $res1 = CommonController::changeCoin($this->getUserId(),ChangeCoinModel::WATCH_VIDEO ,$video_id,'观看视频','+'.$video_coin, $member->coin);
            //观看视频插入数据
            $member->readLook()->save(['type'=> ReadLookModel::TYPE_LOOK,'record_id'=>$video_id]);
            if($res && $res1){
                Db::commit();
                $this->success('成功');
            }else{
                Db::rollback();
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }
}
